#include "PKProgressBar.h"
#include "ui_PKProgressBar.h"
#include <QHBoxLayout>
#include <QDebug>
PKProgressBar::PKProgressBar(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PKProgressBar)
{
    ui->setupUi(this);

}

PKProgressBar::~PKProgressBar()
{
    delete ui;
}


void PKProgressBar::InitUI()
{
    styleFrame_ = new QFrame(this);
    styleFrame_->setGeometry(0,0,this->geometry().width(),this->geometry().height());
    styleFrame_->setFixedSize(this->width(), this->height());

    QHBoxLayout* styleLayout = new QHBoxLayout(styleFrame_);
    styleLayout->setSpacing(0);
    styleLayout->setMargin(0);
    styleLayout->setContentsMargins(0, 0, 0, 0);
    styleFrame_->setLayout(styleLayout);

    leftLabelBar_ = new QLabel(this);
    leftLabelBar_->setObjectName("label_left_process");
    styleLayout->addWidget(leftLabelBar_);
    rightLabelBar_ = new QLabel(this);
    rightLabelBar_->setObjectName("label_right_process");
    styleLayout->addWidget(rightLabelBar_);

    if (nullptr == seperator_label_)
    {
        seperator_label_ = new QLabel(this);
        seperator_label_->setFixedSize(14, 25);
        seperator_label_->setObjectName("label_seperator");
    }


    styleFrame2_ = new QFrame(this);
//    styleFrame2_->setStyleSheet("background-color:green;");
    styleFrame2_->setGeometry(0,0,this->geometry().width(),this->geometry().height());
    styleFrame2_->setFixedSize(this->width(), this->height());

    QHBoxLayout* styleLayout2 = new QHBoxLayout(styleFrame2_);
    styleLayout2->setSpacing(0);
    styleLayout2->setMargin(0);
    styleLayout2->setContentsMargins(0, 0, 0, 0);
    styleFrame2_->setLayout(styleLayout2);

    QSpacerItem *horicalSpacer;
    horicalSpacer = new QSpacerItem(3, 0, QSizePolicy::Fixed, QSizePolicy::Preferred);
    styleLayout2->addItem(horicalSpacer);

        QLabel* leftDiamand = new QLabel(styleFrame2_);
        leftDiamand->setObjectName("label_pink_diamand");
        leftDiamand->setFixedSize(9, 9);
        styleLayout2->addWidget(leftDiamand);

        QSpacerItem *horicalSpacer02;
        horicalSpacer02 = new QSpacerItem(2, 0, QSizePolicy::Fixed, QSizePolicy::Preferred);
        styleLayout2->addItem(horicalSpacer02);

        leftScoreLabel_ = new QLabel(styleFrame2_);
        leftScoreLabel_->setObjectName("label_left_score");
//        leftScoreLabel_->setMinimumSize(80, this->height());
        styleLayout2->addWidget(leftScoreLabel_);

        QSpacerItem *horicalSpacer2;
        horicalSpacer2 = new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Preferred);
        styleLayout2->addItem(horicalSpacer2);

        rightScoreLabel_ = new QLabel(styleFrame2_);
        rightScoreLabel_->setObjectName("label_right_score");
//        rightScoreLabel_->setMinimumSize(80, this->height());
        styleLayout2->addWidget(rightScoreLabel_);

        QSpacerItem *horicalSpacer12;
        horicalSpacer12 = new QSpacerItem(2, 0, QSizePolicy::Fixed, QSizePolicy::Preferred);
        styleLayout2->addItem(horicalSpacer12);

        QLabel* rightDiamand = new QLabel(styleFrame2_);
        rightDiamand->setObjectName("label_blue_diamand");
        rightDiamand->setFixedSize(9, 9);
        styleLayout2->addWidget(rightDiamand);

        QSpacerItem *horicalSpacer3;
        horicalSpacer3 = new QSpacerItem(3, 0, QSizePolicy::Fixed, QSizePolicy::Preferred);
        styleLayout2->addItem(horicalSpacer3);

}

void PKProgressBar::UpdateProgress(float progress, bool bUseValue)
{
    if (bUseValue)
    {
        progressValue_ = progress;
    }
    float realProgress = 0.5;
    if (progressValue_ >= 0.0 && progressValue_ <= 1.0)
    {
        realProgress = (1.0 - 0.85)*0.5 + progressValue_ * 0.85;
    }

    if (nullptr != seperator_label_)
    {
        seperator_label_->setGeometry(realProgress * width() - seperator_label_->width() / 2, -5, seperator_label_->width(), seperator_label_->height());
    }
    leftLabelBar_->setFixedWidth(realProgress * (width()));

}

void PKProgressBar::UpdateScoreLabels(long leftScore, long rightScore)
{
    if (nullptr != leftScoreLabel_)
    {
        leftScoreLabel_->setText(QString("%L1").arg(leftScore));
    }
    if (nullptr != rightScoreLabel_)
    {
        rightScoreLabel_->setText(QString("%L1").arg(rightScore));
    }
}
